package com.ymatou.iapi.deviceid.testcase;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.deviceid.parameter.SaveBean;
import com.ymatou.iapi.deviceid.service.SaveCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.DeviceidCallService;
import com.ymttest.database.mongo.DeviceidBean;
import com.ymttest.database.mongo.DeviceidWapper;
import com.ymttest.utils.TestDataManager;

/**
 * 激活接口用例 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_Save {
	private static SaveBean saveBean;
	private static SaveCall saveCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("激活接口用例");
	}

	@Before
	public void caseUp() {
		saveBean = new SaveBean();
		saveCall = new SaveCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(saveCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_Save_001() {
		Logger.start(true, "激活接口");
		try {
			saveBean = DeviceidCallService.setDefaultFullSaveBean();
			saveCall.setData(saveBean);
			saveCall.callService();

			Logger.verifyEquals("0", saveCall.getCode(), "验证返回Code");
			Logger.verifyEquals("0", saveCall.getBCode(), "验证返回BCode");
			Logger.verifyEquals("saved ok", saveCall.getMSG(), "验证返回Message");
			Logger.verifyEquals("null", saveCall.getData(), "验证返回data");
			
			List<DeviceidBean> deviceidBeans = new DeviceidWapper().getByDeviceid(saveBean.getDeviceid());

			Logger.verifyEquals(1, deviceidBeans.size(), "验证Monggo库生成一条DeviceId记录。");
			
			if(deviceidBeans.size()==1)
			{
				DeviceidBean bean = deviceidBeans.get(0);
				Logger.verifyEquals(saveBean.getAppid(), bean.getAppid(), "验证数据库Appid保存正确");
				Logger.verifyEquals(saveBean.getDeviceid(), bean.getDeviceid(), "验证数据库Deviceid(保存正确");
				Logger.verifyEquals(saveBean.getClienttype(), bean.getClienttype(), "验证数据库Clienttype保存正确");
				Logger.verifyEquals(saveBean.getAppversion(), bean.getAppversion(), "验证数据库Appversion保存正确");
				Logger.verifyEquals(saveBean.getPackageid(), bean.getPackageid(), "验证数据库Packageid保存正确");
				Logger.verifyEquals(saveBean.getIp(), bean.getIp(), "验证数据库Ip保存正确");
				Logger.verifyEquals(saveBean.getUserid(), bean.getUserid(), "验证数据库Userid保存正确");
				Logger.verifyEquals(saveBean.getCcid(), bean.getCcid(), "验证数据库Ccid保存正确");
				Logger.verifyEquals(saveBean.getCkid(), bean.getCkid(), "验证数据库Ckid保存正确");
				Logger.verifyEquals(saveBean.getOsversion(), bean.getOsversion(), "验证数据库Osversion保存正确");
				Logger.verifyEquals(saveBean.getSign(), bean.getSign(), "验证数据库Sign保存正确");
				Logger.verifyEquals(saveBean.getNetwork(), bean.getNetwork(), "验证数据库Network保存正确");
				Logger.verifyEquals(saveBean.getYid(), bean.getYid(), "验证数据库Yid保存正确");
				Logger.verifyEquals(saveBean.getDid(), bean.getDid(), "验证数据库Did保存正确");
				Logger.verifyEquals(saveBean.getIsupdate(), bean.getIsupdate(), "验证数据库Isupdate保存正确");
				
				Logger.verifyNotNull(bean.getUpdateTime(), "验证数据库UpdateTime保存正确");
				
				Logger.verifyEquals(saveBean.getPower(), bean.getPower(), "验证数据库Power保存正确");
				Logger.verifyEquals(saveBean.getAppkey(), bean.getAppkey(), "验证数据库Appkey保存正确");
				Logger.verifyEquals(saveBean.getTimestamp(), bean.getTimestamp(), "验证数据库Timestamp保存正确");
				Logger.verifyEquals(saveBean.getAcceptversion(), bean.getAcceptversion(), "验证数据库Acceptversion保存正确");
				Logger.verifyEquals(saveBean.getMac(), bean.getMac(), "验证数据库Mac保存正确");
				Logger.verifyEquals(saveBean.getSignVerified(), bean.getSignVerified(), "验证数据库SignVerified保存正确");
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_Save_002() {
		Logger.start(true, "激活接口");
		try {
			saveBean = DeviceidCallService.setDefaultSaveBean();
			saveCall.setData(saveBean);
			saveCall.callService();

			Logger.verifyEquals("0", saveCall.getCode(), "验证返回Code");
			Logger.verifyEquals("0", saveCall.getBCode(), "验证返回BCode");
			Logger.verifyEquals("saved ok", saveCall.getMSG(), "验证返回Message");
			Logger.verifyEquals("null", saveCall.getData(), "验证返回data");
			
			List<DeviceidBean> deviceidBeans = new DeviceidWapper().getByDeviceid(saveBean.getDeviceid());

			Logger.verifyEquals(1, deviceidBeans.size(), "验证Monggo库生成一条DeviceId记录。");
			
			if(deviceidBeans.size()==1)
			{
				DeviceidBean bean = deviceidBeans.get(0);
				Logger.verifyEquals(saveBean.getAppid(), bean.getAppid(), "验证数据库Appid保存正确");
				Logger.verifyEquals(saveBean.getDeviceid(), bean.getDeviceid(), "验证数据库Deviceid(保存正确");
				Logger.verifyEquals(saveBean.getClienttype(), bean.getClienttype(), "验证数据库Clienttype保存正确");
			}
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_Save_003() {
		Logger.start(true, "激活接口-参数空");
		try {
//			saveBean = DeviceidCallService.setDefaultSaveBean();
			saveCall.setData(saveBean);
			saveCall.callService();

			Logger.verifyEquals("0", saveCall.getCode(), "验证返回Code");
			Logger.verifyEquals("0", saveCall.getBCode(), "验证返回BCode");
			Logger.verifyEquals("saved ok", saveCall.getMSG(), "验证返回Message");
			Logger.verifyEquals("null", saveCall.getData(), "验证返回data");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_Save_004() {
		Logger.start(true, "激活接口-参数重复");
		try {
			saveBean = DeviceidCallService.setDefaultSaveBean();
			saveCall.setData(saveBean);
			saveCall.callService();
			
			saveCall.setData(saveBean);
			saveCall.callService();

			Logger.verifyEquals("0", saveCall.getCode(), "验证返回Code");
			Logger.verifyEquals("0", saveCall.getBCode(), "验证返回BCode");
			Logger.verifyEquals("saved ok", saveCall.getMSG(), "验证返回Message");
			Logger.verifyEquals("null", saveCall.getData(), "验证返回data");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_Save_005() {
		Logger.start(true, "激活接口-保存设备号时根据deviceId和did查询验证通过的设备号，如果找到则不做任何操作返回，否则生成新的设备号。");
		try {
			saveBean = DeviceidCallService.setDefaultFullSaveBean();
			saveCall.setData(saveBean);
			saveCall.callService();

			List<DeviceidBean> deviceidBeans = new DeviceidWapper().getByDeviceid(saveBean.getDeviceid());
			Logger.verifyEquals(1, deviceidBeans.size(), "验证Monggo库生成一条DeviceId记录。");
			
			if(deviceidBeans.size()==1)
			{
				DeviceidBean bean = deviceidBeans.get(0);
				Logger.verifyEquals(saveBean.getAppid(), bean.getAppid(), "验证数据库Appid保存正确");
				Logger.verifyEquals(saveBean.getDeviceid(), bean.getDeviceid(), "验证数据库Deviceid(保存正确");
				Logger.verifyEquals(saveBean.getClienttype(), bean.getClienttype(), "验证数据库Clienttype保存正确");
				Logger.verifyEquals(saveBean.getAppversion(), bean.getAppversion(), "验证数据库Appversion保存正确");
				Logger.verifyEquals(saveBean.getPackageid(), bean.getPackageid(), "验证数据库Packageid保存正确");
				Logger.verifyEquals(saveBean.getIp(), bean.getIp(), "验证数据库Ip保存正确");
				Logger.verifyEquals(saveBean.getUserid(), bean.getUserid(), "验证数据库Userid保存正确");
				Logger.verifyEquals(saveBean.getCcid(), bean.getCcid(), "验证数据库Ccid保存正确");
				Logger.verifyEquals(saveBean.getCkid(), bean.getCkid(), "验证数据库Ckid保存正确");
				Logger.verifyEquals(saveBean.getOsversion(), bean.getOsversion(), "验证数据库Osversion保存正确");
				Logger.verifyEquals(saveBean.getSign(), bean.getSign(), "验证数据库Sign保存正确");
				Logger.verifyEquals(saveBean.getNetwork(), bean.getNetwork(), "验证数据库Network保存正确");
				Logger.verifyEquals(saveBean.getYid(), bean.getYid(), "验证数据库Yid保存正确");
				Logger.verifyEquals(saveBean.getDid(), bean.getDid(), "验证数据库Did保存正确");
				Logger.verifyEquals(saveBean.getIsupdate(), bean.getIsupdate(), "验证数据库Isupdate保存正确");
				
				Logger.verifyNotNull(bean.getUpdateTime(), "验证数据库UpdateTime保存正确");
				
				Logger.verifyEquals(saveBean.getPower(), bean.getPower(), "验证数据库Power保存正确");
				Logger.verifyEquals(saveBean.getAppkey(), bean.getAppkey(), "验证数据库Appkey保存正确");
				Logger.verifyEquals(saveBean.getTimestamp(), bean.getTimestamp(), "验证数据库Timestamp保存正确");
				Logger.verifyEquals(saveBean.getAcceptversion(), bean.getAcceptversion(), "验证数据库Acceptversion保存正确");
				Logger.verifyEquals(saveBean.getMac(), bean.getMac(), "验证数据库Mac保存正确");
				Logger.verifyEquals(saveBean.getSignVerified(), bean.getSignVerified(), "验证数据库SignVerified保存正确");
			}
			
			saveCall.setData(saveBean);
			saveCall.callService();
			
			Logger.verifyEquals("0", saveCall.getCode(), "验证返回Code");
			Logger.verifyEquals("0", saveCall.getBCode(), "验证返回BCode");
			Logger.verifyEquals("saved ok", saveCall.getMSG(), "验证返回Message");
			Logger.verifyEquals("null", saveCall.getData(), "验证返回data");
			
			List<DeviceidBean> deviceidBeans2 = new DeviceidWapper().getByDeviceid(saveBean.getDeviceid());
			Logger.verifyEquals(1, deviceidBeans2.size(), "验证Monggo库生成一条DeviceId记录。");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_Save_006() {
		Logger.start(true, "JIRACONN-17427激活接口增加machinename字段");
		try {
			saveBean = DeviceidCallService.setDefaultFullSaveBean();
			saveBean.setMachinename("iphoneX");
			saveCall.setData(saveBean);
			saveCall.callService();

			Logger.verifyEquals("0", saveCall.getCode(), "验证返回Code");
			Logger.verifyEquals("0", saveCall.getBCode(), "验证返回BCode");
			Logger.verifyEquals("saved ok", saveCall.getMSG(), "验证返回Message");
			Logger.verifyEquals("null", saveCall.getData(), "验证返回data");
			
			List<DeviceidBean> deviceidBeans = new DeviceidWapper().getByDeviceid(saveBean.getDeviceid());

			Logger.verifyEquals(1, deviceidBeans.size(), "验证Monggo库生成一条DeviceId记录。");
			
			if(deviceidBeans.size()==1)
			{
				DeviceidBean bean = deviceidBeans.get(0);
				Logger.debug(com.ymt.core.tool.FormatUtil.beanToGSONString(deviceidBeans));
				Logger.verifyEquals(saveBean.getAppid(), bean.getAppid(), "验证数据库Appid保存正确");
				Logger.verifyEquals(saveBean.getDeviceid(), bean.getDeviceid(), "验证数据库Deviceid(保存正确");
				Logger.verifyEquals(saveBean.getClienttype(), bean.getClienttype(), "验证数据库Clienttype保存正确");
				Logger.verifyEquals(saveBean.getAppversion(), bean.getAppversion(), "验证数据库Appversion保存正确");
				Logger.verifyEquals(saveBean.getPackageid(), bean.getPackageid(), "验证数据库Packageid保存正确");
				Logger.verifyEquals(saveBean.getIp(), bean.getIp(), "验证数据库Ip保存正确");
				Logger.verifyEquals(saveBean.getUserid(), bean.getUserid(), "验证数据库Userid保存正确");
				Logger.verifyEquals(saveBean.getCcid(), bean.getCcid(), "验证数据库Ccid保存正确");
				Logger.verifyEquals(saveBean.getCkid(), bean.getCkid(), "验证数据库Ckid保存正确");
				Logger.verifyEquals(saveBean.getOsversion(), bean.getOsversion(), "验证数据库Osversion保存正确");
				Logger.verifyEquals(saveBean.getSign(), bean.getSign(), "验证数据库Sign保存正确");
				Logger.verifyEquals(saveBean.getNetwork(), bean.getNetwork(), "验证数据库Network保存正确");
				Logger.verifyEquals(saveBean.getYid(), bean.getYid(), "验证数据库Yid保存正确");
				Logger.verifyEquals(saveBean.getDid(), bean.getDid(), "验证数据库Did保存正确");
				Logger.verifyEquals(saveBean.getIsupdate(), bean.getIsupdate(), "验证数据库Isupdate保存正确");
				
				Logger.verifyNotNull(bean.getUpdateTime(), "验证数据库UpdateTime保存正确");
				
				Logger.verifyEquals(saveBean.getPower(), bean.getPower(), "验证数据库Power保存正确");
				Logger.verifyEquals(saveBean.getAppkey(), bean.getAppkey(), "验证数据库Appkey保存正确");
				Logger.verifyEquals(saveBean.getTimestamp(), bean.getTimestamp(), "验证数据库Timestamp保存正确");
				Logger.verifyEquals(saveBean.getAcceptversion(), bean.getAcceptversion(), "验证数据库Acceptversion保存正确");
				Logger.verifyEquals(saveBean.getMac(), bean.getMac(), "验证数据库Mac保存正确");
				Logger.verifyEquals(saveBean.getSignVerified(), bean.getSignVerified(), "验证数据库SignVerified保存正确");
				Logger.verifyEquals(saveBean.getMachinename(), bean.getMachinename(), "验证数据库machinename保存正确");
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}